//
//  PhotoItemViewController.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/25.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "AssetItemViewController.h"
#import "PhotoScrollView.h"
#import "Masonry.h"
#import "UIImage+CTAssetsPickerController.h"
#import "VideoView.h"

@interface AssetItemViewController ()

@property (nonatomic, strong) PhotoScrollView *photoView;
@property (nonatomic, strong) VideoView *videoView;
@property (nonatomic, strong) UIButton *playButton;

@end

@implementation AssetItemViewController

+ (AssetItemViewController *)assetItemViewControllerForPageIndex:(NSInteger)pageIndex;
{
    return [[self alloc] initWithPageIndex:pageIndex];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self.dataSource respondsToSelector:@selector(mediaAtIndex:)]) {
        Media *media = [self.dataSource mediaAtIndex:self.pageIndex];
        
        if (media.isVideo.boolValue) {
            self.videoView   = [[VideoView alloc] initWithFrame:self.view.frame];
            self.videoView.dataSource           = self.dataSource;
            self.videoView.index                = self.pageIndex;
            
            self.view = self.videoView;
            
            [self addVideoPlayButton];
        } else {
            self.photoView   = [[PhotoScrollView alloc] initWithFrame:self.view.frame];
            self.photoView.dataSource           = self.dataSource;
            self.photoView.index                = self.pageIndex;
            
            self.view = self.photoView;
        }
        
        [self addGestureRecognizers];
    }
}

- (id)initWithPageIndex:(NSInteger)pageIndex
{
    if (self = [super init]) {
        self.pageIndex = pageIndex;
    }
    
    return self;
}

- (UIView *)imageViewInAssetView
{
    if ([self.dataSource respondsToSelector:@selector(mediaAtIndex:)]) {
        Media *media = [self.dataSource mediaAtIndex:self.pageIndex];
        
        if (media.isVideo.boolValue) {
            return self.videoView.imageView;
        } else {
            return self.photoView.imageView;
        }
    }
    
    return nil;
}

- (void)setFullResolutionImage:(UIImage *)image
{
    if (self.photoView) {
        self.photoView.imageView.image = image;
    }
}

- (BOOL)isVideo
{
    if ([self.dataSource respondsToSelector:@selector(mediaAtIndex:)]) {
        Media *media = [self.dataSource mediaAtIndex:self.pageIndex];
        return media.isVideo.boolValue;
    }
    
    return NO;
}

#pragma mark - Gesture Recognizer

- (void)addGestureRecognizers
{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapping:)];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapping:)];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    [doubleTap setNumberOfTapsRequired:2.0];
    [singleTap requireGestureRecognizerToFail:doubleTap];
    
    [self.view addGestureRecognizer:singleTap];
    [self.view addGestureRecognizer:doubleTap];
    [self.view addGestureRecognizer:longPress];
}

- (void)handleTapping:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.numberOfTapsRequired == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if (recognizer.numberOfTapsRequired == 2) {
        if (self.view == self.videoView) {
            ;
        } else {
            [self.photoView toggleZooming:recognizer];
        }
    }
}

- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        if ([self.delegate respondsToSelector:@selector(longPress:)]) {
            [self.delegate longPress:self];
        }
    }
}

- (void)addVideoPlayButton
{
    UIImage *image   = [UIImage ctassetsPickerControllerImageNamed:@"CTAssetsPickerPlay"];
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.playButton setImage:image forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.playButton];
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
    }];
}

- (void)playVideo:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(playVideo:)]) {
        [self.delegate playVideo:self];
    }
}

- (void)setVideoState:(PlayVideoState)state
{
    if ([self.delegate respondsToSelector:@selector(playerController)]) {
        MPMoviePlayerController *player = [self.delegate playerController];
        
        if (PlayVideoStatePlaying == state) {
            player.controlStyle = MPMovieControlStyleFullscreen;
            
            [self.videoView.imageView setHidden:YES];
            [self.playButton setHidden:YES];
            [player.view setHidden:NO];
        } else if (PlayVideoStateFinished == state) {
            player.controlStyle = MPMovieControlStyleNone;
            
            [self.videoView.imageView setHidden:NO];
            [self.playButton setHidden:NO];
            [player.view setHidden:YES];
        }
    }
}

@end
